package com.ruoyi.web.controller.dysms;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.modules.domain.Credit;
import com.ruoyi.modules.service.CreditService;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

import static com.ruoyi.web.controller.dysms.SmsDemo.querySendDetails;

/**
 * 阿里云短信服务, 用于发送贷款返校校验码
 */
@Api(tags = "阿里云短信服务")
@RestController
@RequestMapping("/aliyun")
public class DysmsController {

    @Autowired
    private CreditService conCreditService;

    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 发送校验码
     * @param creditId 贷款编号
     * @param userId 用户编号
     * @return
     * @throws IOException
     * @throws ClientException
     * @throws InterruptedException
     */
    @ApiOperation("发送校验码")
    @PostMapping("/sendSms")
    @Log(title = "发送校验码", businessType = BusinessType.UPDATE)
    public AjaxResult sendSms(@RequestParam("creditId") Long creditId, @RequestParam("userId") Long userId) throws IOException, ClientException, InterruptedException {
        Credit credit = conCreditService.selectCreditById(creditId);
        //发短信
        SendSmsResponse response = SmsDemo.sendSms();
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());

        Thread.sleep(3000L);

        //查明细
        if(response.getCode() != null && response.getCode().equals("OK")) {
            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
            System.out.println("短信明细查询接口返回数据----------------");
            System.out.println("Code=" + querySendDetailsResponse.getCode());
            System.out.println("Message=" + querySendDetailsResponse.getMessage());
            int i = 0;
            for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
            {
                System.out.println("SmsSendDetailDTO["+i+"]:");
                System.out.println("Content=" + smsSendDetailDTO.getContent());
                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
            }
            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
        }

            AjaxResult ajax = AjaxResult.success();
            ajax.put("message", "发送校验码成功!");
            ajax.put("rebackCode", response.getCode());
            // 更新
            credit.setRebackCode(SmsDemo.CURRENTCODE);
            conCreditService.updateCredit(credit);

            // 保存到公告通知
            SysNotice sysNotice = new SysNotice();
            sysNotice.setNoticeTitle("返校验证码");
            sysNotice.setNoticeType("短信通知");
            sysNotice.setNoticeContent("尊敬的用户，您的注册会员动态密码为: " + SmsDemo.CURRENTCODE + ", 请勿泄漏于他人！");
            sysNotice.setStatus("成功");
            sysNotice.setUserId(userId);
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            sysNotice.setCreateBy(authentication.getName());
            sysNotice.setCreateTime(new Date());
            noticeService.insertNotice(sysNotice);

        return ajax;
    }

    /**
     * 注册时发送短信验证码
     * @return
     */
    @PostMapping("/toSendRegistrySms")
    public AjaxResult toSendRegistrySms(@RequestParam("phone") String phone) throws ClientException, InterruptedException {

        AjaxResult ajax = AjaxResult.success();
        //发短信
        SendSmsResponse response = SmsDemo.sendRegistrySms(phone);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());

        Thread.sleep(3000L);

        //查明细
        if(response.getCode() != null && response.getCode().equals("OK")) {
            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(response.getBizId());
            System.out.println("短信明细查询接口返回数据----------------");
            System.out.println("Code=" + querySendDetailsResponse.getCode());
            System.out.println("Message=" + querySendDetailsResponse.getMessage());
            int i = 0;
            for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs())
            {
                System.out.println("SmsSendDetailDTO["+i+"]:");
                System.out.println("Content=" + smsSendDetailDTO.getContent());
                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
            }
            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
        }

        ajax.put("code", 200);
        ajax.put("sms", SmsDemo.CURRENTCODE);

        return ajax;
    }

}
